<template>
  <div class="app-container" v-loading="isSyncLoading" :element-loading-text="isSyncLoadingText">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="fbillno">
        <el-input v-model="queryParams.fbillno" placeholder="请输入单据编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单据状态" prop="fdocumentStatus">
        <el-select v-model="queryParams.fdocumentStatus" placeholder="请选择单据状态" clearable>

        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="fdate">
        <el-date-picker clearable v-model="queryParams.fdate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="synchronization" size="mini"
          v-hasPermi="['wms:return:sync']">同步数据</el-button>
        <!-- <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wm:goods:export']">导出</el-button> -->
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="goodsList" :span-method="objectSpanMethod" height="calc(100vh - 230px)">
      <el-table-column label="序号" align="center" width="50px" prop="issueCode">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据编号" align="center" width="140" prop="fbillno">
        <!-- <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)" v-hasPermi="['mes:pro:workorder:query']">{{
            scope.row.fbillno }}</el-button>
        </template> -->
      </el-table-column>
      <el-table-column label="日期" align="center" width="150px" prop="fdate" />
      <el-table-column label="产品名称" width="150px" align="center" prop="itemName" />
      <el-table-column label="产品编码" width="150px" align="center" prop="itemCode" />
      <el-table-column label="产品规格" width="150px" align="center" prop="specification" />
      <el-table-column label="批号" width="150px" align="center" prop="flot" />
      <el-table-column label="技术分解单号" width="150px" align="center" prop="ftlsnText1" />
      <el-table-column label="合同号" width="150px" align="center" prop="ftlsnText" />
      <el-table-column label="订单号" width="150px" align="center" prop="fOrderNo" />
      <el-table-column label="退货客户" align="center" width="200px" prop="clientName" />
      <el-table-column label="退货原因" align="center" width="200px" prop="freturnReason">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" :disabled="scope.row.pushDownFlag != 0"
            @click="pushdown(scope.row.id)" v-hasPermi="['wms:return:push']">下推</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script type="javascript">
import { sync, list, pushdown } from '@/api/mes/wm/returnNotice'
export default {
  name: "ReturnGoods",
  data() {
    return {
      showSearch: true,
      loading: false,
      queryParams: {
        fbillno: "",
        fdate: '',
        fdocumentStatus: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      goodsList: [],
      isSyncLoading: false,
      isSyncLoadingText: ""
    };
  },
  mounted() {
    console.log("mounted")
    this.handleQuery();
  },
  methods: {
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 12) {
        console.log(row.num, rowIndex)
        if (row.num) {
          return {
            rowspan: row.num,
            colspan: 1
          };
        }
      }
    },
    pushdown(id) {
      this.$modal
        .confirm(
          '是否确认下推该条数据？'
        )
        .then(() => {
          pushdown(id).then(res => {
            if (res.code == 200) {
              this.$message.success("下推成功");
              this.getList();
            }
          })
        })
    },
    resetQuery() {
      this.$refs.queryForm.resetFields();
    },
    synchronization() {
      this.isSyncLoading = true
      this.isSyncLoadingText = '正在同步中，请稍后……'
      sync().then(res => {
        if (res.code == 200) {
          this.isSyncLoading = false
          this.$message.success("同步成功");
          this.getList();
        }
      }).catch(() => {
        this.isSyncLoading = false
      });
    },
    init(row) {
      this.list = row;
    },
    getList() {
      this.loading = true;
      list(this.queryParams).then(res => {
        if (res.code == 200) {
          let index;
          let num, id;
          //res.rows = res.rows.filter(item=>item.fid==100029)
          res.rows.forEach((item, i) => {
            if (id != item.fid) {
              id = item.fid;
              if (index != undefined) {
                res.rows[index].num = num == 1 ? 0 : num;
              }
              num = 1;
              index = i;
            } else {
              num++;
            }

            if (i == res.rows.length - 1) {
              res.rows[index].num = num == 1 ? 0 : num;
            }
          })
          this.goodsList = res.rows;
          this.total = res.total;
        }
        this.loading = false;
      })
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    }
  }
};
</script>